package com.cxf.windex.rest;

import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.util.WxUtil;
import com.cxf.windex.biz.JsUserBiz;
import com.cxf.windex.entity.JsUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: business_needs
 * @description: 推广二维码
 * @author: chenxf
 * @create: 2018-11-09 15:21
 **/
@Slf4j
@Api(tags = {"通用 - 推广二维码接口"})
@Controller
@RequestMapping("/user/qrCode")
public class QrCodeController {

  @Autowired
  private JsUserBiz jsUserBiz;

  /**
   * 文件存放 路径
   */
  @Value("${upload.path}")
  private String uploadPath;

  /**
   * 文件访问路径
   */
  @Value("${upload.url}")
  private String uploadUrl;

  /**
   * 文件访问域名
   */
  @Value("${upload.uri}")
  private String uploadUri;

  @Value("${weixin.appid}")
  private String lAppid;
  @Value("${weixin.secret}")
  private String lSecret;
  @Value("${fw.appid}")
  private String fAppid;
  @Value("${fw.secret}")
  private String fSecret;

  @ApiOperation(value = "查询资金信息", notes = "根据openid查询资金信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "range", value = " 0商家 1村长 默认商家", required = true, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "wxopenid", value = "微信 openID", required = true, paramType = "query", dataType = "String")
  })
  @RequestMapping(value = "/getCode", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse getCode(String wxopenid,@RequestParam(defaultValue = "0")int range) {
    return new ObjectRestResponse("生成成功").rel(true).result(mkCode(wxopenid,range));
  }

  public String mkCode(String wxopenid,int range){
    String userId=jsUserBiz.queryByOpenid(wxopenid).getId();
    String webRoot =
        uploadPath + File.separator + "mina" + File.separator ;
    String saveUrl =
        uploadUri + uploadUrl + "/mina/"  + userId+range + ".jpg";
    File directoryFile = new File(webRoot);
    if (!directoryFile.exists()) {
      directoryFile.mkdirs();
    }
    File codeFile = new File(webRoot + userId +range+ ".jpg");
    if (codeFile.exists()) {
      return saveUrl;
    }
    Path path = Paths.get(webRoot + userId +range+ ".jpg");
    try {
      Map<String, Object> map = new HashMap<>(2);
      map.put("scene", UserConstant.SENCE+ userId);
      map.put("width",860);
      map.put("is_hyaline",true);
      String appid=lAppid,secret=lSecret;
      if(range==1){
        appid=fAppid;secret=fSecret;
      }
      byte[] codeBytes = WxUtil.getMinaCodeB(appid, secret, map);
      Files.write(path, codeBytes);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return saveUrl;
  }

}
